$(function () {
    //演示多文件列表
    var uploadListIns = layui.upload.render({
        elem: '#upload-drag'
        , url: "/upload/temp/image.json"
        , accept: 'images'
        , multiple: true
        //,number: 3
        //,auto: false
        //,bindAction: '#view-pics'
        , choose: function (obj) {

        }
        , done: function (res, index, upload) {
            if (res.code === 0) { //上传成功
                if (selectList.indexOf(res.data.oldPath) < 0) {
                    pictureList.push(res.data.src);
                    selectList.push(res.data.oldPath);
                    var tr = $(['<div class="upload-item">'
                        , '<img onclick="viewImage(this.src)" src="' + res.data.src + '" alt="' + res.data.src + '" class="upload-img">'
                        , '<span class="demo-delete" title="删除"><i class="fa fa-remove"></i></span>'
                        , '</div>'].join(''));
                    //删除
                    tr.find('.demo-delete').on('click', function () {
                        var fIndex = pictureList.indexOf(res.data.src);
                        if (fIndex > -1) {
                            pictureList.splice(fIndex, 1);
                        }
                        fIndex = selectList.indexOf(res.data.oldPath);
                        if (fIndex > -1) {
                            selectList.splice(fIndex, 1);
                        }
                        tr.remove();
                    });
                    $("#view-pics").append(tr);
                }
            } else {
                return layer.msg("上传失败：" + res.msg);
            }
        }
        , allDone: function (obj) {
            console.log(obj)
        }
        , error: function (index, upload) {
            console.log(obj)
        }
    });
    /*分类-新增-选择父部门树*/
    $('#categoryName').click(function () {
        var options = {
            title: null,
            width: '380px',
            url: "/category/select.html?categoryId=" + $("#categoryId").val() + "&channelId=" + channelId,
            callBack: function (index, layero) {
                var body = layer.getChildFrame('body', index);
                setCategory(body.find('#categoryId').val(), body.find('#categoryName').val())
                layer.close(index);
            }
        };
        $.modal.openOptions(options);
    });
});

function setCategory(id, name) {
    $("#categoryId").val(id);
    $("#categoryName").val(name);
}

function submitPicture() {
    var categoryId = $("#categoryId").val();
    if (categoryId.length < 1 || categoryId === "0") {
        return layer.msg("请先选择所属分类！");
    }
    if ($("#title").val().length < 3) {
        return layer.msg("请输入标题，且长度不能少于3个字符！");
    }
    if (pictureList.length < 1) {
        return layer.msg("请先上传图片！");
    }
    var data = $("#form-picture-add").serializeArray();
    data.push({"name": "pictures", "value": pictureList.join(",")});
    var loading = layer.load(1);
    $.ajax({
        url: "/picture/add.json",
        type: "post",
        dataType: "json",
        data: data,
        timeout: 180000,
        success: function (result) {
            layer.close(loading);
            $.operate.successCallback(result);
        }
    });
}